import { useRequest } from 'umi';
import { requestFunComponFiex, getUserDepID, getTenantID } from '@/utils';
import { getBaseUrl } from '@/services/urlconst';
import { MethodsChangeTab } from '../mconfig';
import { ILoadMapTypeAll } from '@/common';

const baseUrl = getBaseUrl();
// const baseUrl = '/xc';
export const ChangeMethodManageService = () => {
  /**管理页列表接口 */
  const getChangeMethodManageService = (curRes) => {
    const request = useRequest(
      (requestParams?: Record<string, any>) => {
        let params: any = {
          current: 1,
          pageSize: 10,
          sort: 'createTime',
          order: 'desc',
          dicTypeIds: `${getTenantID()}${ILoadMapTypeAll.PURCHASE_CHANGEWAY},${getTenantID()}${ILoadMapTypeAll.ALLFLOW_CHECKRESULT}`,
          ...MethodsChangeTab(curRes),
          ...requestParams,
        };
        const reqUrl = `${baseUrl}/purchaseSourceChangeController/dataGrid/${params?.params}`;
        delete params['applyFormNameDept'];
        delete params['applyFormNameUser'];
        if (params['createrID']?.length) {
          params['createrID'] = params['createrID']?.toString();
        } else {
          delete params['createrID'];
        }
        delete params['applyFormNameDept'];
        delete params['applyFormNameUser'];
        if (params?.applyerID && params?.applyerID?.length > 0) {
          params['applyerID'] = params?.applyerID?.toString();
        } else {
          delete params['applyerID'];
        }
        if (params?.applyTime) {
          const [applyTimeStart, applyTimeEnd] = params.applyTime;
          params['applyTimeStart'] = applyTimeStart.format('YYYY-MM-DD 00:00:00');
          params['applyTimeEnd'] = applyTimeEnd.format('YYYY-MM-DD 23:59:59');
          delete params['applyTime'];
        }
        return requestFunComponFiex('POST', reqUrl, params);
      },
      {
        manual: false,
        paginated: true,
        defaultPageSize: 10,
      },
    );
    return {
      changeMethodManageRun: request.run,
      changeMethodManageDataSource: request.data?.rows || [],
      changeMethodManageLoading: request.loading,
      changeMethodManageTableProps: request.tableProps,
      reloadRequest: request?.refresh,
    };
  };

  return {
    getChangeMethodManageService,
  };
};
